#include<bits/stdc++.h>
#define ll long long
#define pn putchar('\n')
#define fls() fflush(stdout)
#define mset(a,x) memset(a,x,sizeof a)
#define mcpy(a,b) msmcpy(a,b,sizeof a)
#define all(a) a.begin(),a.end()
#define maxn 1000005
using namespace std;
void gx(ll &x,ll y)
{
	if(y>x)
		x=y;
}
int n;
int a[maxn];
ll f[maxn];
void solve()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	mset(f,192);
	f[0]=0;
	ll tag=0,mx=0;
	for(int i=2;i<=n;i++)
	{
		int ad=0;
		if(a[i]==a[i-1])
			ad=a[i];
		ll t=max(mx,f[a[i]]+a[i]);
		gx(f[a[i-1]],t-ad);
		gx(mx,f[a[i-1]]);
		tag+=ad;
	}
	ll ans=0;
	for(int i=0;i<=1e6;i++)
		gx(ans,f[i]+tag);
	printf("%lld\n",ans);
}
int main()
{
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	int T;
	scanf("%d",&T);
	while(T--)
		solve();
	return 0;
}